package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.itsource.basic.config.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.JsonResult;
import cn.itsource.basic.util.LoginInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.*;
import cn.itsource.pet.mapper.OrderPetAcquisitionMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.query.OrderPetAcquisitionQuery;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.query.SearchMasterMessageQuery;
import cn.itsource.pet.service.IOrderPetAcquisitionService;
import cn.itsource.pet.service.IPetService;
import cn.itsource.pet.service.IPetTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
//Propagation.SUPPORTS:支持别人事务
//readOnly = true : 只读型事务
//propagation = Propagation.NOT_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet, PetQuery> implements IPetService {
    @Resource
    private PetMapper petMapper;
    @Override
    public PageList<Pet> findPage(PetQuery petQuery) {
        PageHelper.startPage(petQuery.getPage(),petQuery.getSize());
        List<Pet> page = petMapper.findPage(petQuery);
        PageInfo<Pet> pageInfo = new PageInfo<>(page);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void petShelve(Long id) {
        Pet pet = petMapper.selectByPrimaryKey(id);
        if(pet.getState()==1){
            throw new BusinessException("此商品已经上架");
        }
        pet.setState(1);
        pet.setOnsaletime(new Date());
        petMapper.updateByPrimaryKeySelective(pet);
    }

    @Override
    public void downShelve(Long id) {
        Pet pet = petMapper.selectByPrimaryKey(id);
        pet.setState(0);
        petMapper.updateByPrimaryKeySelective(pet);
    }

    @Override
    public void save(Pet pet) {
        pet.setState(0);
        petMapper.insertSelective(pet);
    }

    @Override
    public PageList<Pet> queryHomePage(PetQuery petQuery) {
        PageHelper.startPage(petQuery.getPage(),petQuery.getSize());
        List<Pet> page = petMapper.findHome(petQuery);
        PageInfo<Pet> pageInfo = new PageInfo<>(page);
        return new PageList<>(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public Pet petDetail(Long id) {
        Pet pet = petMapper.selectByPrimaryKey(id);
        return pet;
    }

    @Override
    public PetForm queryTypeNames() {
        PetForm petForm = new PetForm();
        List<PetType> petTypes = petMapper.selectForm();
        ArrayList<String> typeNames= new ArrayList<>();
        ArrayList<Long> labList= new ArrayList<>();
        for (PetType petType : petTypes) {
            String name = petType.getName();
            typeNames.add(name);
        }
        for (PetType petType : petTypes) {
            Long count = petType.getCount();
            labList.add(count);
        }
        petForm.setTypeNames(typeNames);
        petForm.setLabList(labList);
        return petForm;
    }

    @Override
    public PetTypeForm formObject() {
        PetTypeForm petTypeForm = new PetTypeForm();
        List<Pet> pets = petMapper.selectPet();
        ArrayList<String> typePet= new ArrayList<>();
        for (Pet pet : pets) {
            String name = pet.getName();
            typePet.add(name);
        }
        ArrayList<PetCount> petCounts = new ArrayList<>();
        petTypeForm.setTypePet(typePet);
        for (Pet pet : pets) {
            PetCount petCount = BeanUtil.copyProperties(pet, PetCount.class);
            petCounts.add(petCount);
        }
        petTypeForm.setPetTypeCounts(petCounts);

        return petTypeForm;
    }

}

